<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 写一个函数，格式化时间 formatDate(),参数可以是字符串，也可以是时间对象。
效果： 2021-10-05 15：15：09 星期几 -->
<script type="text/javascript">
	//定义一个函数
    function formatDate(){
		//获取当前时间
        let Time = new Date();

        let nYear = Time.getFullYear();
        let nMonth = Time.getMonth() + 1; 
        let nDate = Time.getDate();
        let nHour = Time.getHours();
        let nMinutes = Time.getMinutes();
        let nSeconds = Time.getSeconds();
        let nDay = Time.getDay();
        //判断若数小于10时在前面加0，例：10-05
		if(nYear<10){
			nYear ="0"+nYear;
		}
		if(nMonth<10){
			nMonth ="0"+nMonth;
		}
		if(nDate<10){
			nDate ="0"+nDate;
		}
		if(nHour<10){
			nHour ="0"+nHour;
		}
		if(nMinutes<10){
			nMinutes ="0"+nMinutes;
		}
		if(nSeconds<10){
			nSeconds ="0"+nSeconds;
		}
		
		//用文字输出星期几
		if(nDay == 0){
			nDay = "日";
		}
		else if(nDay == 1){
			nDay = "一";
		}
		else if(nDay == 2){
			nDay = "二";
		}
		else if(nDay == 3){
			nDay = "三";
		}
		else if(nDay == 4){
			nDay = "四";
		}
		else if(nDay == 5){
			nDay = "五";
		}
		else if(nDay == 6){
			nDay = "六";
		}
		
        let ThisTime = `${nYear}-${nMonth}-${nDate} ${nHour}:${nMinutes}:${nSeconds} 星期${nDay}`
        return ThisTime
    }
    document.write(formatDate());
</script>
</body>
</html>
